/***************************************************************************
 * Copyright (C) 2005 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.agi.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;

/**
 * The usage of a Genus by a AGIDatasource
 * @author trobertson
 */
@Entity
@Table(name="genus_usage")
public class GenusUsage extends BaseObject implements Comparable<Object> {
	private static final long serialVersionUID = -8832189111467890180L;
	
	private Long id;
	private AGIDatasource agiDatasource;
	private Genus genus;	
	private RemoteId remoteId;
	private Date created = new Date();
	private Date updated = new Date();
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(columnDefinition="int")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne(optional=false)
	@JoinColumn(name="datasource_id")
	public AGIDatasource getAgiDatasource() {
		return agiDatasource;
	}
	public void setAgiDatasource(AGIDatasource agiDatasource) {
		this.agiDatasource = agiDatasource;
	}
	
	@Column(nullable=false)
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	
	@Column(nullable=false)
	public Date getUpdated() {
		return updated;
	}
	public void setUpdated(Date updated) {
		this.updated = updated;
	}
	
	@ManyToOne(optional=false)
	@JoinColumn(name="genus_id")
	public Genus getGenus() {
		return genus;
	}
	public void setGenus(Genus genus) {
		this.genus = genus;
	}
	
	@OneToOne(mappedBy="usage")
	public RemoteId getRemoteId() {
		return remoteId;
	}
	public void setRemoteId(RemoteId remoteId) {
		this.remoteId = remoteId;
	}
	
	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		GenusUsage myClass = (GenusUsage) object;
		return new CompareToBuilder().append(this.created, myClass.created)
				.append(this.remoteId,
						myClass.remoteId).append(this.agiDatasource,
						myClass.agiDatasource).append(this.updated,
						myClass.updated).append(this.genus, myClass.genus)
				.append(this.id, myClass.id).toComparison();
	}
	
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof GenusUsage)) {
			return false;
		}
		GenusUsage rhs = (GenusUsage) object;
		return new EqualsBuilder().append(this.created, rhs.created)
				.append(this.remoteId, rhs.remoteId)
				.append(this.agiDatasource, rhs.agiDatasource).append(
						this.updated, rhs.updated)
				.append(this.genus, rhs.genus).append(this.id, rhs.id)
				.isEquals();
	}
	
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1544062499, 699936683).append(this.created)
				.append(this.remoteId).append(
						this.agiDatasource).append(this.updated).append(
						this.genus).append(this.id).toHashCode();
	}
	
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this)
			.append("id", this.id)
			.append("genus", this.genus)
			.append("agiDatasource", this.agiDatasource)
			.append("updated", this.updated)				
			.append("created", this.created)
			.append("remoteId", this.remoteId)
			.toString();
	}
}
